package com.bw.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bw.entity.Dict;
import com.bw.entity.JobAddParam;
import com.bw.entity.JobInfo;
import com.bw.entity.JobQuery;
import com.bw.mapper.JobMapper;
import com.bw.service.JobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class ListController {

    @Autowired
    JobMapper jobMapper;
    @Autowired
    JobService jobService;

    @RequestMapping("/list.do")
    public String list(JobQuery query,@RequestParam(defaultValue = "1") Integer pageNum,Model model) {
        // 1. 下拉框
        selectData(model);
        // 2.分页查询
        PageHelper.startPage(pageNum,3);
        List<JobInfo> list = jobMapper.selectPage(query);
        PageInfo<JobInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        // 3.条件回显
        model.addAttribute("query",query);
        return "list";
    }

    /**
     * 单删 批删
     * @param id
     * @return
     */
    @RequestMapping("del.do")
    public String del(String id){

        jobMapper.delById(id);

        return "redirect:list.do";

    }

    /**
     * 添加页面
     * @param model
     * @return
     */
    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        // 1. 下拉框
        selectData(model);
        return "add";
    }

    /**
     * 执行添加
     * @param model
     * @return
     */
    @RequestMapping("/add.do")
    public String add(@Valid JobAddParam addParam, BindingResult result,Model model){
        // 1. 非空验证
        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //给下拉框赋值
            selectData(model);
            return "add";
        }
        //员工编号以“GH”开始
        String num = addParam.getNum();
        if (!num.startsWith("GH")){
            model.addAttribute("error","员工编号以“GH”开始");
            //给下拉框赋值
            selectData(model);
            return "add";
        }
        //开始时间小于结束时间
        String startTime = addParam.getStartTime();
        // 获取时间戳
        long startTimeParse = DateUtil.parse(startTime, "yyyy-MM-dd").getTime();

        String endTime = addParam.getEndTime();
        long endTimeParse = DateUtil.parse(endTime, "yyyy-MM-dd").getTime();

        if (startTimeParse > endTimeParse){
            model.addAttribute("error","开始时间小于结束时间");
            //给下拉框赋值
            selectData(model);
            return "add";
        }

        //保存方法事务，保证原子性
        jobService.addJob(addParam);
        return "redirect:list.do";
    }


    /**
     * 给下拉框赋值
     * @param model
     */
    private void selectData(Model model){
        List<Dict> postList = jobMapper.listPost();
        model.addAttribute("postList", postList);
        List<Dict> schoolList = jobMapper.listSchool();
        model.addAttribute("schoolList", schoolList);
        List<Dict> deptList = jobMapper.listDept();
        model.addAttribute("deptList", deptList);


    }

}
